<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Templates extends API_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('sites_model');
        $this->load->model('templates_model');
        $this->load->model('site_template_model');
        $this->load->model('home_model');
        $this->load->model('introduction_model');
        $this->load->model('product_model');
    }

    /**
	 * 模板列表
	 * @param $site_id
	 */
	public function get_template_list()
    {
        $site_id = $this->input->get('site_id');
        if(!$site_id){
        	error_output('基本信息ID验证失败.');
        }
        //@$cat_id 模板分类
        $cat_id = 0;
        //查询所有模板
        $tpls = $this->templates_model->get_template_list();
        if(empty($tpls)){
        	error_output('加载模板列表出错,请尝试重新加载!');
        }
        //得到模板id
        $tpl_one_id = $this->input->get('t_id');
        if(!$tpl_one_id) {
	        //取第一个模板id
	        if(isset($tpls[0]['id'])){
	        	$tpl_one_id = $tpls[0]['id'];
	        }else{
	        	error_output('加载模板出错,请尝试重新加载!');
	        }
        }
        //查询单个模板
        $single_tpls = $this->templates_model->get_template_by_id($tpl_one_id);
        $data = array(
        	'site_id' => $site_id,
        	't_id' => $tpl_one_id,
        	'tpls' => $tpls,
        	'single_tpls' => $single_tpls,
        	'url' => "index.php/templates/get_template_list",
        	'action' => "index.php/templates/template_matching",
        );
        $this->load->view("templates/template.php", $data);
    }
    
    /**
     * 模板配色
     * @param site_id  t_id  color
     */
    public function template_matching()
    {
    	$site_id = $this->input->post('site_id');
    	$t_id = $this->input->post('t_id');
    	$color = $this->input->get('color');
    	$color = "0";
    	if(!$site_id){
    		error_output('基本信息ID验证失败.');
    	}
    	if(!$t_id){
    		error_output('请选择模板.');
    	}
    	//查询单个模板
    	$single_tpls = $this->templates_model->get_template_by_id($t_id);
    	if(empty($single_tpls)){
    		error_output('加载模板出错,请尝试重新加载!');
    	}
    	if($color){
    		foreach ($single_tpls as &$val){
    			$val['img_path'] = $this->change_path($val['img_path'], $color);
    		}
    	}
    	$data = array(
        	'site_id' => $site_id,
        	't_id' => $t_id,
        	'single_tpls' => $single_tpls,
    		'color'=> $color,
    		'action' => "index.php/templates/set_templates",
        );
        $this->load->view("templates/template2.php", $data);
    }
    
    /**
     * 模板确认
     * @param  $site_id $t_id $color
     * @return string
     */
    public function set_templates()
    {
    	$site_id = $this->input->post('site_id');
    	$t_id = $this->input->post('t_id');
    	$color = $this->input->post('color') ? $this->input->post('color') : '1';
    	if(!$site_id){
    		error_output('基本信息ID验证失败.');
    	}
    	if(!$t_id){
    		error_output('请选择模板.');
    	}
    	$inser_data = array(
    		'site_id' => $site_id,
    		't_id' => $t_id,
    		'color' => $color,
    		'create_time' => time(),
    	);
    	
    	$insert_id = $this->site_template_model->insert($inser_data);
    	if(!$insert_id){
    		error_output('保存模板失败.');
    	}
    	
		//$this->set_home_template($site_id, $t_id, $color);
		header("Location:".base_url()."index.php/templates/set_home_template?site_id=".$site_id."&t_id=".$t_id."&color=".$color);
    	//$this->load->view("template.php", $data);
    }
    
    /**
     * 设置首页内容
     */
    public function set_home_template()
    {
    	//首页type为0
    	$site_id = $this->input->get('site_id');
    	$t_id = $this->input->get('t_id');
    	$color = $this->input->get('color');
    	$data = $this->home_model->get_home_by_site_id($site_id);
    	$type = 0;
    	$single_tpls = $this->templates_model->get_template_by_id_type($t_id);
    	$tpl = $single_tpls['html_path'];
    	if(!$data){
	    	$data = array(
	    			'site_id' => $site_id,
	    			't_id' => $t_id,
	    			'item1' => '公司介绍',
	    			'item2' => '特色项目',
	    			'item3' => '进行评价',
	    			'item4' => '常见问题',
	    			'create_time' => time(),
	    	);
	    	
	    	$insert_id = $this->home_model->insert($data);
	    	$data['id'] = $insert_id;
    	}
	$data['color'] = $color;
	if($this->input->get('baseurl')){
		$data['static'] = 1;
		$data['base_url'] = $this->input->get('baseurl');
	}else{
		$data['base_url'] = base_url()."/index.php/templates/set_item";
	}
	$sites = $this->sites_model->get_site_by_id($site_id);
	if(isset($sites['name'])){
		$data['site_name'] = $sites['name'];
	}
	$this->load->view($tpl, $data);
    	
    }
    /**
     * 设置首页图片
     */
    public function set_home_image()
    {
	$site_id = $this->input->get('site_id');
    	$t_id = $this->input->get('t_id');
    	$color = $this->input->get('color');
    	$data = $this->home_model->get_home_by_site_id($site_id);
    	$type = 0;
    	$single_tpls = $this->templates_model->get_template_by_id_type($t_id);
	$img_name = $this->input->post('img_name');
	if($img_name) {
		$change_data = array('img_name' => $img_name);
		//判断是否有上传图片
		if (isset($_FILES['picture']['tmp_name']) && is_uploaded_file($_FILES['picture']['tmp_name'])) {
			//上传
			$pic_data = file_upload('picture', 'picture');
			if ($pic_data) {
				$change_data['img_path'] = $pic_data['path'];
				$update_id = $this->home_model->update($site_id, $change_data);
			} else {
				error_output(validation_error_one('picture upload fail'));
			}
		}
		header("Location:".base_url()."index.php/templates/set_item1?site_id=".$site_id."&t_id=".$t_id."&color=".$color);exit;
	}	
        $this->load->view('templates/homeimgedit.php', $data);
        
    }
    /**
     * 设置栏目一
     */
    public function set_item1()
    {
    	$site_id = $this->input->get('site_id');
    	$t_id = $this->input->get('t_id');
    	$color = $this->input->get('color');
   	$template = $this->get_item_template($t_id, 1);
   	$tpl = $template['html_path'];
    	$td_id = $template['id'];
	$data = array('site_id' => $site_id, 't_id' => $t_id, 'td_id' => $td_id);
	if($this->input->post('title') and $this->input->post('content')){
		$data['title'] = $this->input->post('title');	
		$data['contents'] = $this->input->post('content');
		//判断是否有上传图片
		if (isset($_FILES['picture']['tmp_name']) && is_uploaded_file($_FILES['picture']['tmp_name'])) {
			//上传
			$pic_data = file_upload('picture', 'picture');
			if ($pic_data) {
				$data['img_path'] = $pic_data['path'];
			} else {
				error_output(validation_error_one('picture upload fail'));
			}
		}
		$result = $this->introduction_model->get_intro_by_id($site_id);
		if($result){
			$data['create_time'] = time();
			$insert_id = $this->introduction_model->update($site_id, $data);
		}else{
			$data['create_time'] = time();
			$insert_id = $this->introduction_model->insert($data);
		}
		header("Location:".base_url()."index.php/templates/set_item2?site_id=".$site_id."&t_id=".$t_id."&color=".$color);
	}else{
		$result = $this->introduction_model->get_intro_by_id($site_id);
		if($result){
			$data = $result;
		}
		$data['color'] = $color;
		
		if($this->input->get('static')){
			$data['static'] = 1;
		}
		$this->load->view($tpl, $data);
	}
    }
    
    /**
     * 设置栏目二
     */
    public function set_item2()
    {
    	$site_id = $this->input->get('site_id');
    	$t_id = $this->input->get('t_id');
    	$color = $this->input->get('color');
    	$template = $this->get_item_template($t_id, 2);
    	$tpl = $template['html_path'];
    	$td_id = $template['id'];
   		$data = array('site_id' => $site_id, 't_id' => $t_id, 'td_id' => $td_id);
   		
    	if($this->input->post('title') and $this->input->post('content')){
    		$data['title'] = $this->input->post('title');
    		$data['contents'] = $this->input->post('content');
    		$result = $this->product_model->get_intro_by_id($site_id);
    		if($result){
    			$insert_id = $this->product_model->update($site_id, $data);
    		}else{
    			$insert_id = $this->product_model->insert($data);
    		}
    		header("Location:".base_url()."index.php/templates/set_item3?site_id=".$site_id."&t_id=".$t_id."&color=".$color);
    	}else{
    		$result = $this->product_model->get_intro_by_id($site_id);
    		if($result){
    			$data = $result;
    		}
    		$data['color'] = $color;
    		if($this->input->get('static')){
    			$data['static'] = 1;
    		}
    		$this->load->view($tpl, $data);
    	}
    }
    
    /**
     * 设置栏目三
     */
    public function set_item3()
    {
    	$site_id = $this->input->get('site_id');
        $t_id = $this->input->get('t_id');
        $color = $this->input->get('color');
    	$data = array(
    		'site_id' => $site_id,
            't_id' => $t_id,
            'color' => $color,
    	);
    	if($this->input->get('static')){
    		$data['static'] = 1;
    	}
    	$this->load->view("templates/item3.php", $data);
    }
    
    /**
     * 设置栏目四
     */
    public function set_item4()
    {
    	$site_id = $this->input->get('site_id');
        $t_id = $this->input->get('t_id');
        $color = $this->input->get('color');
        $data = array(
            'site_id' => $site_id,
            't_id' => $t_id,
            'color' => $color,
        );
        if($this->input->get('static')){
            $data['static'] = 1;
        }

    	$this->load->view("templates/item4.php", $data);
    }

    /**
     * 生成网站成功
     */
    public function site_success()
    {
        $this->load->view("templates/sitesuccess.php");
    }
    
    public function get_item_template($t_id, $type)
    {
    	$single_tpls = $this->templates_model->get_template_by_id_type($t_id, $type);
    	return $single_tpls;
    }
    
    
    public function change_path($path, $color)
    {
    	$pathinfo = pathinfo($path);
    	$filename = $pathinfo['filename'].'_'.$color;
    	return $pathinfo['dirname'].'/'.$filename.'.'.$pathinfo['extension'];
    }

}
